package ch.randstand.terror.xmlsocketclient
{
	
	import ch.randstand.terror.xmlsocketclient.XMLSocketClientObject;
	
	
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	
	import flash.net.XMLSocket;
	
	import flash.events.Event;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	
	
	///   get it here: http://www.boostworthy.com/blog/?p=170
	import com.boostworthy.animation.management.AnimationManager;
	import com.boostworthy.animation.easing.Transitions;
	import com.boostworthy.animation.rendering.RenderMethod;
	
	
	[SWF(frameRate="60", backgroundColor="0x000000")]

	public class XMLSocketClient extends Sprite
	{
		
		private var object				: XMLSocketClientObject = new XMLSocketClientObject();
		private var objects				: Array					= new Array();
		
		private var host				: String				= "localhost";
		private var port				: int					= 8888;
		
		private var xmlsocket			: XMLSocket				= new XMLSocket();
		
		private var animationManager 	: AnimationManager		= new AnimationManager
		
		public function XMLSocketClient()
		{
			init();
		}
		
		private function init() : void
		{
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align		= StageAlign.TOP_LEFT;
			
			stage.addEventListener( MouseEvent.CLICK, initMove );
			
			addChild( object );
			
			object.x = Math.random() * stage.stageWidth;
			object.y = Math.random() * stage.stageHeight;
			
			xmlsocket.addEventListener( Event.CONNECT, onConnect );
			xmlsocket.addEventListener( DataEvent.DATA, onData );
			
			xmlsocket.connect( host, port );
			
		}
		
		private function onConnect( event : Event ) : void
		{
			
			var data : XML =
				<data>
					<type>newUser</type>
					<id>{ object.getID() }</id>
					<color>{ object.getColor() }</color>
					<xpos>{ object.x }</xpos>
					<ypos>{ object.y }</ypos>
				</data>;
			
			xmlsocket.send( data );
			
		}
		
		private function onData( event : DataEvent ) : void
		{
			
			var data : XML = XML( event.data );
			
			switch( data.type.toString() )
			{
				
				case "newUser":
				
					if( data.id == object.getID() )
					{
						
					} else {
						var o : XMLSocketClientObject = new XMLSocketClientObject( data.id, data.color );
						o.x = data.xpos;
						o.y = data.ypos;
						
						objects[ data.id ] = o;
						addChild( o );
					}
					
					var d : XML =
						<data>
							<type>existingUser</type>
							<id>{ object.getID() }</id>
							<color>{ object.getColor() }</color>
							<xpos>{ object.x }</xpos>
							<ypos>{ object.y }</ypos>
						</data>;
						
					xmlsocket.send( d );
					
					break;
				
				case "existingUser":
				
					if( data.id == object.getID() || objects[ data.id ] != null )
					{
						
					} else {
						var oo : XMLSocketClientObject = new XMLSocketClientObject( data.id, data.color );
						oo.x = data.xpos;
						oo.y = data.ypos;
						
						objects[ data.id ] = oo;
						addChild( oo );
					}
					
					break;
					
				case "movement":
				
					move( objects[ data.id ], data.xpos, data.ypos );
					
					break;
				
			}
			
		}
		
		private function initMove( event : MouseEvent ) : void
		{
			move( object, stage.mouseX, stage.mouseY );
			
			var data : XML =
				<data>
					<type>movement</type>
					<id>{ object.getID() }</id>
					<xpos>{ stage.mouseX }</xpos>
					<ypos>{ stage.mouseY }</ypos>
				</data>;
			
			xmlsocket.send( data );
			
		}
		
		private function move( _o : *, _x : Number, _y : Number ) : void
		{
			animationManager.move( _o, _x, _y, 250, Transitions.SINE_OUT, RenderMethod.TIMER );
		}
	}
}